
import {Component,Input,Output} from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators ,ReactiveFormsModule } from '@angular/forms';
import { StudentcurdComponent } from './Student-curd.component';
import { HttpClient } from '@angular/common/http';
import { SysConfig } from '../System/SystemConfig';

@Component({
    selector: 'student-detail',
    templateUrl:'student-detail.component.html',
    styleUrls:["student-detail.component.css"]
    })
    export class StudentdetailComponent{

    parentComponent:StudentcurdComponent;
    constructor(private http: HttpClient) {}
    form=new FormGroup({

        Id : new FormControl('',[    ]),
        Name : new FormControl('',[Validators.required, Validators.maxLength(20), Validators.minLength(2),  ]),
        Phone : new FormControl('',[    ]),
        Birthday : new FormControl('',[Validators.required,    ]),
        Address : new FormControl('',[ Validators.maxLength(50),   ]),
        Email : new FormControl('',[Validators.required,   Validators.email, ]),
        Age : new FormControl('',[Validators.required,    ]),
        Avatar : new FormControl('',[    ]),
        Password : new FormControl('',[    ]),
        Url : new FormControl('',[    ]),
        Regular : new FormControl('',[    Validators.pattern(/[A-z]{3}/),]),
        History : new FormControl('',[Validators.required, Validators.maxLength(100),   ]),
        Appendix : new FormControl('',[    ]),
        Getup : new FormControl('',[    ]),

    });

    //getters for fields

            get Id() {return this.form.get('Id');}
            get Name() {return this.form.get('Name');}
            get Phone() {return this.form.get('Phone');}
            get Birthday() {return this.form.get('Birthday');}
            get Address() {return this.form.get('Address');}
            get Email() {return this.form.get('Email');}
            get Age() {return this.form.get('Age');}
            get Avatar() {return this.form.get('Avatar');}
            get Password() {return this.form.get('Password');}
            get Url() {return this.form.get('Url');}
            get Regular() {return this.form.get('Regular');}
            get History() {return this.form.get('History');}
            get Appendix() {return this.form.get('Appendix');}
            get Getup() {return this.form.get('Getup');}

    setValue(data)
    {
        this.form.patchValue(data);
    }

    createClick()
    {
        if (this.form.valid)
        {
            this.parentComponent.showDetailWithData(this.form.value);
        }
    }

    saveClick()
    {
    if(this.form.valid)
    {
        this.http.post(SysConfig.apiRoot+'/student',this.form.value,SysConfig.httpOptions)
        .subscribe(resp => {
            console.log(resp);
        },
        errResp=>{
            console.log("http error:");
            console.log(errResp);
        });
    }
    }   //-- saveClick

    cancelClick()
    {
        this.parentComponent.childCancel(this);
    }   //-- cancelClick


    }
